package net.helix.engine.library.items;

import org.lwjgl.util.vector.Vector3f;

import net.helix.core.Bounds;
import net.helix.core.geometry.Geometry_Material;
import net.helix.core.geometry.Geometry_Mesh;
import net.helix.core.geometry.Geometry_Model;
import net.helix.core.transform.Transform;
import net.helix.core.transform.TransformDimension;
import net.helix.core.transform.ViewTransform;
import net.helix.element.Element;
import net.helix.element.Element_Mesh;
import net.helix.element.Element_Model;
import net.helix.element.ViewNodeElement;
import net.helix.engine.Engine;
import net.helix.engine.library.Library;
import net.helix.engine.properties.Properties;
import net.helix.engine.properties.Properties_Material;
import net.helix.engine.properties.Properties_Mesh;
import net.helix.engine.viewPath.ViewNode;
import net.helix.engine.viewPath.ViewPath;
import net.helix.graphics.GraphicsEngine;
import net.helix.graphics.GraphicsHelper;
import net.helix.physics.Collidable3d;
import net.helix.physics.Collision;

public class LibraryItem_Mesh extends LibraryItem {
	private Geometry_Model model;
	private Geometry_Mesh mesh;
	private Geometry_Material material;
	public LibraryItem_Mesh(Library library, String name) {
		super(library, name);
		this.type = LibraryItemType.MESH;
		
		model = null;
		mesh = null;
		material = null;
	}
	
	public void setModel(Geometry_Model model){
		this.model = model;
	}
	public void setMesh(Geometry_Mesh mesh){
		this.mesh = mesh;
	}
	public void setMaterial(Geometry_Material material){
		this.material = material;
	}
	
	public Geometry_Mesh getMesh(){
		return mesh;
	}
	public Geometry_Material setMaterial(){
		return material;
	}
	
	@Override
	public void render(Properties properties) {
		//material.applyProperties((Properties_Material) properties);
		mesh.render(model, material);
	}
	
	public void renderSphere(Properties properties) {
		//material.applyProperties((Properties_Material) properties);
		mesh.renderSphere(model, material);
	}
	public void renderBoudingBox(Properties properties) {
		//material.applyProperties((Properties_Material) properties);
		mesh.renderBoudingBox();
	}
	
	@Override
	public void renderLibrary(Engine engine, int width, int height) {
		ViewPath path = new ViewPath();
		
		Element_Mesh model = (Element_Mesh) createElement();
		
		path.setRoot(new ViewNode((ViewNodeElement) model, 0));
		
		ViewTransform viewTransform = new ViewTransform();
		
		Transform transform = new Transform(TransformDimension.THREE);
		
		
		Bounds bounds = mesh.getBounds();
		
		float distance = Math.max(Math.max(bounds.getWidth(), bounds.getHeight()), bounds.getDepth());
		
		distance = distance*.75f;
		
		transform.setTranslate(-bounds.getMeanX(), -bounds.getMeanY(), -bounds.getMeanZ() - distance);
		
		viewTransform.setTransform(transform);
		
		GraphicsEngine.switchRoot3D(engine, viewTransform, path, width, height, true);
		
		path.render();
		
	}

	@Override
	public Properties makeDefaultProperties() {
		return new Properties_Mesh();
	}

	@Override
	public Element createElement() {
		return new Element_Mesh(this);
	}

}
